package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.repository.StrategyCommentRepsitory;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;


@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private StrategyCommentRepsitory strategyCommentRepsitory;

    @Autowired
    private MongoTemplate template;




    //容易让mongodb产生误会,后续自动生成的id不会存进去
    @Override
    public void save(StrategyComment strategyComment) {
        strategyComment.setId(null);
        strategyCommentRepsitory.save(strategyComment);
    }

    @Override
    public void update(StrategyComment strategyComment) {
        strategyCommentRepsitory.save(strategyComment);
    }

    @Override
    public void delete(String id) {
        strategyCommentRepsitory.deleteById(id);
    }

    @Override
    public StrategyComment get(String id) {
        return strategyCommentRepsitory.findById(id).orElse(null);
    }

    @Override
    public List<StrategyComment> list() {
        return strategyCommentRepsitory.findAll();
    }

    //游记评论页面
    @Override
    public Page<StrategyComment> query(StrategyCommentQuery qo) {
        //创建查询条件
        Query query = new Query();
        //存入查询条件
        if (StringUtils.hasLength(qo.getStrategyId())){
            query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));
        }
        //根据id分页(升序)
        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize()
                        ,Sort.Direction.ASC,"_id");

        return DBHelper.query(query,StrategyComment.class,template,pageable);
    }

    /**
     * 攻略评论点赞
     * @param cid:评论id
     * @param sid:攻略id
     */
    @Override
    public void commentThumb(String cid, String sid, UserInfo userInfo) {
        //通过cid获取评论对象
        StrategyComment comment = this.get(cid);
        //获取点赞集合,Thumbuplist是攻略评论表的点赞用户id集合
        List<String> thumbuplist = comment.getThumbuplist();
        //判断用户id是否存在集合中
        if (thumbuplist.contains(userInfo.getId())){
            //在就取消点赞,点赞数-1
            comment.setThumbupnum(comment.getThumbupnum()-1);
            //移除出集合
            thumbuplist.remove(userInfo.getId());
        }else {
            //不在就点赞,点赞数+1
            comment.setThumbupnum(comment.getThumbupnum()+1);
            //添加进集合
            thumbuplist.add(userInfo.getId());
        }
        this.update(comment);
    }



}

